#include "Console.h"
#include "../util/Util.h"
#include <windows.h>


namespace WindGE
{
	Console::Console(void)
	{
		AllocConsole();
		fopen_s(&file_, "Log.txt", "a+");
		_tsetlocale(LC_ALL, _T("chs"));

	}

	Console::~Console(void)
	{
		fclose(file_);
		FreeConsole();

	}

	void log_print(const std::wstring& type, const std::wstring& buf)
	{
		std::wclog << type << buf << std::endl;
	}

	void Console::log_error(wchar_t const* fmt, ...)
	{

		va_list args;
		va_start(args, fmt);
		wchar_t buf[1024];
		memset(buf, 0, 1024);
		(void)vswprintf(buf, fmt, args);
		va_end(args);
		std::wstring typestr = L"(ERROR) ";
		std::wstring bufstr(buf);
		fputws((typestr + bufstr + L"\n").c_str(), file_);
		log_print(typestr, bufstr);
	}

	void Console::log_warning(wchar_t const* fmt, ...)
	{

		va_list args;
		va_start(args, fmt);
		wchar_t buf[1024];
		memset(buf, 0, 1024);
		(void)vswprintf(buf, fmt, args);
		va_end(args);
		std::wstring typestr = L"(WARN) ";
		std::wstring bufstr(buf);
		fputws((typestr + bufstr + L"\n").c_str(), file_);
		log_print(typestr, bufstr);
	}

	void Console::log(wchar_t const* fmt, ...)
	{
		va_list args;
		va_start(args, fmt);
		wchar_t buf[1024];
		memset(buf, 0, 1024);
		(void)vswprintf(buf, fmt, args);
		va_end(args);
		std::wstring typestr = L"(INFO) ";
		std::wstring bufstr;

		fputws((typestr + bufstr + L"\n").c_str(), file_);
		log_print(typestr, bufstr);
	}

	void Console::start_loop()
	{
		//thread_.join();
	}

	void Console::loop()
	{
		while (true)
		{

		}

	}
}